package nc.bs.levmpoundmanager.poundpushbill;

import java.util.ArrayList;
import java.util.List;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.levmmeampoundbill.poundbill.ILevmPoundBillService;
import nc.itf.levmpoundmanager.poundpushbill.ILevmPoundPushBill;
import nc.itf.mmgp.uif2.IMMGPCmnQueryService;
import nc.vo.levmmeampoundbill.poundbill.AggPoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillBVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PushBillStatusEnum;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmgp.util.MMArrayUtil;
import nc.vo.mmgp.util.MMListUtil;
import nc.vo.mmgp.util.MMStringUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;

public class LevmPoundPushBillService implements ILevmPoundPushBill {
	private ILevmPoundBillService poundBillService;
	private IMMGPCmnQueryService service;

	public Integer poundPushBill(String[] pkPoundBill) throws BusinessException {
		int successNum = 0;
		if (MMArrayUtil.isEmpty(pkPoundBill)) {
			return Integer.valueOf(0);
		}
		Object[] arrayAggbypks = getAggVOByPks(pkPoundBill);
		if (MMArrayUtil.isEmpty(arrayAggbypks)) {
			return Integer.valueOf(0);
		}
		for (Object oldaggObject : arrayAggbypks) {
			try {
				AggPoundBillVO[] aggVos = getAggVO((AggPoundBillVO) oldaggObject);
				List<AggPoundBillVO> listaggvo = new ArrayList<AggPoundBillVO>();
				StringBuffer msg=new StringBuffer();
				for (AggPoundBillVO agg : aggVos) {
					AggPoundBillVO aggVo = getPoundBillService().pushOrWritePoundBill_RequiresNew(agg);
					listaggvo.add(aggVo);
					PoundBillVO headvo=	aggVo.getParentVO();
					msg.append(headvo.getVpushbillinfo());
				}
				if (isPushBillSucess(listaggvo)) {
					successNum++;
				}
//				else {
//					if(msg!=null &&msg.length()>0) {
//						ExceptionUtils.wrappBusinessException(msg.toString());
//					}
//				}
			} catch (BusinessException e) {
				Logger.error(e);
				ExceptionUtils.wrappBusinessException(e.getMessage());
			}
		}
		return Integer.valueOf(successNum);
	}

	private Object[] getAggVOByPks(String[] pkPoundBill) throws BusinessException {
		SqlBuilder sql = new SqlBuilder();
		sql.append("pk_poundbill", pkPoundBill);

		return getQueryService().cmnQueryDatasByConditionLazyLoad(AggPoundBillVO.class, sql.toString(), false);
	}

	private boolean isPushBillSucess(List<AggPoundBillVO> listaggvo) throws BusinessException {
		if (listaggvo == null || listaggvo.size() == 0) {
			return false;
		}
		for (AggPoundBillVO aggvo : listaggvo) {
			String pushInfo = aggvo.getParentVO().getVpushbillinfo();
			PoundBillBVO[] bVos = aggvo.getChildrenVO();
			if (MMArrayUtil.isEmpty(bVos)) {
				throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0152"));
			}
			for (PoundBillBVO bVo : bVos) {
				if (PushBillStatusEnum.PUSH_BILL_YET.value().equals(bVo.getNpushbillstatus())) {
					return false;
				}
			}
			if (!MMStringUtil.isEmpty(pushInfo)) {
				return false;
			}
		}
		return true;
	}

	private AggPoundBillVO[] getAggVO(AggPoundBillVO aggVo) throws BusinessException {
		List<AggPoundBillVO> Listaggvo = new ArrayList<AggPoundBillVO>();

		AggPoundBillVO nagAggVo = new AggPoundBillVO();
		List<PoundBillBVO> bList = new ArrayList<PoundBillBVO>();
		List<PoundBillBVO> nagBList = new ArrayList<PoundBillBVO>();
		PoundBillBVO[] bVos = aggVo.getChildrenVO();
		if (MMArrayUtil.isEmpty(bVos)) {
			throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0152"));
		}
		for (PoundBillBVO bVo : bVos) {
			if (PushBillStatusEnum.PUSH_BILL_YET.value().equals(bVo.getNpushbillstatus()) && bVo.getNnum() != null) {

				if (UFDouble.ZERO_DBL.compareTo(bVo.getNnum()) >= 0) {
					nagBList.add(bVo);
				} else {
					bList.add(bVo);
				}
			}
		}
		nagAggVo.setParentVO(aggVo.getParentVO());
		nagAggVo.setChildrenVO((CircularlyAccessibleValueObject[]) nagBList.toArray(new PoundBillBVO[0]));
		aggVo.setChildrenVO((CircularlyAccessibleValueObject[]) bList.toArray(new PoundBillBVO[0]));
		if (!MMListUtil.isEmpty(bList)) {
			Listaggvo.add(aggVo);
		}
		if (!MMListUtil.isEmpty(nagBList)) {
			Listaggvo.add(nagAggVo);
		}
		return (AggPoundBillVO[]) Listaggvo.toArray(new AggPoundBillVO[0]);
	}

	private ILevmPoundBillService getPoundBillService() {
		if (this.poundBillService == null) {
			this.poundBillService = (ILevmPoundBillService) NCLocator.getInstance().lookup(ILevmPoundBillService.class);
		}
		return this.poundBillService;
	}

	private IMMGPCmnQueryService getQueryService() {
		if (this.service == null) {
			this.service = (IMMGPCmnQueryService) NCLocator.getInstance().lookup(IMMGPCmnQueryService.class);
		}
		return this.service;
	}
}
